package com.coffee.order.service.sku.impl;

import com.alibaba.fastjson.JSONObject;
import com.coffee.admin.api.platform.query.PlatformSystemDeployQuery;
import com.coffee.admin.service.merchant.bean.Merchant;
import com.coffee.admin.service.merchant.dao.MerchantDao;
import com.coffee.admin.service.platform.bean.PlatformSystemDeploy;
import com.coffee.admin.service.platform.dao.PlatformSystemDeployDao;
import com.coffee.core.entity.Page;
import com.coffee.core.enums.*;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.DateUtil;
import com.coffee.daili.api.query.DailiMerchantQuery;
import com.coffee.daili.service.bean.DailiMerchant;
import com.coffee.daili.service.dao.DailiMerchantDao;
import com.coffee.order.api.sku.query.*;
import com.coffee.order.api.sku.req.PayReq;
import com.coffee.order.api.sku.result.*;
import com.coffee.order.api.sku.service.CommonPayService;
import com.coffee.order.service.sku.bean.*;
import com.coffee.order.service.sku.dao.*;
import com.coffee.sku.service.sku.bean.Sku;
import com.coffee.sku.service.sku.bean.SkuCategory;
import com.coffee.sku.service.sku.bean.SkuContent;
import com.coffee.sku.service.sku.bean.SkuSalesInformation;
import com.coffee.sku.service.sku.dao.SkuCategoryDao;
import com.coffee.sku.service.sku.dao.SkuContentDao;
import com.coffee.sku.service.sku.dao.SkuDao;
import com.coffee.sku.service.sku.dao.SkuSalesInformationDao;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@DubboService(dynamic = true)
public class PayServiceImpl implements CommonPayService {
    private final static Logger logger = LoggerFactory.getLogger(PayServiceImpl.class);
    @Value("${parameter.webDomain}")
    public String webDomain;

    @Value("${parameter.weixin.appId}")
    public String appId;

    @Value("${parameter.weixin.appSecret}")
    public String appSecret;

    @Value("${TAG}")
    private String environment;

    @Value("${enjouyDay}")
    private String enjouyDay;


    @Resource
    private PayOrderDao payOrderDao;
    @Resource
    private OrderDao orderDao;
    @Resource
    private SkuOrderDao skuOrderDao;
    @Resource
    private DailiMerchantDao dailiMerchantDao;
    @Resource
    private MerchantDao merchantDao;
    @Resource
    private SkuOrderDetailDao skuOrderDetailDao;
    @Resource
    private SkuOrderDetailPeriodizationDao skuOrderDetailPeriodizationDao;
    @Resource
    private SkuSalesInformationDao skuSalesInformationDao;
    @Resource
    private PlatformSystemDeployDao platformSystemDeployDao;
    @Resource
    private SkuDao skuDao;
    @Resource
    private SkuContentDao skuContentDao;
    @Resource
    private SkuCategoryDao skuCategoryDao;
    @Resource
    private OrderRelationDao orderRelationDao;
    @Autowired
    private SkuOrderDeliveryDemandDao skuOrderDeliveryDemandDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PayParam createOrderAndPay(PayReq req) throws BusinessException {
        logger.info("PayReq===" + JSONObject.toJSONString(req));
        String payMethod = req.getPayMethod();
        PayOrder order = payOrderDao.findByMerchantIdAndOutOrderId(req.getUserId(), req.getOutOrderId());
        Date date = new Date();
        if (order == null) {
            order = new PayOrder();
            order.setTitle(req.getSubject());
            order.setMoney(req.getAmount());
            order.setOutOrderId(req.getOutOrderId());
            order.setMerchantId(req.getUserId());
            order.setPayMethod(payMethod);
            order.setIsPay("N");
            order.setCreateTime(date);
            payOrderDao.insert(order);
        } else {
            PayOrder updateOrder = new PayOrder();
            updateOrder.setTitle(req.getSubject());
            updateOrder.setMoney(req.getAmount());
            updateOrder.setOutOrderId(req.getOutOrderId());
            updateOrder.setId(order.getId());
            payOrderDao.updateById(updateOrder);
        }
        if ("Y".equals(order.getIsPay())) {
            throw new BusinessException(ExceptionEnum.ERROR_1800.getCode(), ExceptionEnum.ERROR_1800.getMessage());
        }
        if (order.getOutOrderId().startsWith(environment + SkuOrderTypeEnum.ENJOY.getCode())) {
            OrderRelation orderRelation = new OrderRelation();
            orderRelation.setPayOrderId(order.getId());
            orderRelation.setSkuOrderId(Integer.parseInt(order.getOutOrderId().split("-")[1]));
            orderRelation.setCreateTime(new Date());
            orderRelationDao.insert(orderRelation);
        }
        PayParam payParam = new PayParam();
        payParam.setAmount(req.getAmount());
        payParam.setOutOrderId(req.getOutOrderId());
        payParam.setSubject(req.getSubject());
        payParam.setPrepareId(req.getOrderTypeEnum().getCode() + "-" + order.getId());
        payParam.setPayMethod(payMethod);
        payParam.setOrderId(order.getId() + "");
        return payParam;
    }

    @Override
    public PayParam findByOrderId(Integer merchantId, String outOrderId) throws BusinessException {
        PayOrder payOrder = payOrderDao.findByMerchantIdAndOutOrderId(merchantId, outOrderId);
        if (payOrder == null) {
            throw new BusinessException(ExceptionEnum.ERROR_1801.getCode(), ExceptionEnum.ERROR_1801.getMessage());
        }
        if ("Y".equals(payOrder.getIsPay())) {
            throw new BusinessException(ExceptionEnum.ERROR_1800.getCode(), ExceptionEnum.ERROR_1800.getMessage());
        }
        Integer orderId = null;
        String[] split = outOrderId.split("-");
        if (split.length > 1) {
            orderId = Integer.parseInt(split[1]);
        }
        if (orderId == null) {
            throw new BusinessException(ExceptionEnum.ERROR_1801.getCode(), ExceptionEnum.ERROR_1801.getMessage());
        }
        Double payPrice = 0.0;
        String isModify = "N";
        if (outOrderId.startsWith(environment + OrderTypeEnum.SKU.getCode())) {
            SkuOrder skuOrder = skuOrderDao.findById(orderId);
            if (skuOrder == null) {
                throw new BusinessException(ExceptionEnum.ERROR_1801.getCode(), ExceptionEnum.ERROR_1801.getMessage());
            }
            if (SkuOrderStatusEnum.CANCEL.getCode().equals(skuOrder.getStatus())) {
                throw new BusinessException(ExceptionEnum.ERROR_1847.getCode(), ExceptionEnum.ERROR_1847.getMessage());
            }
            if ("Y".equals(skuOrder.getIsModifyPrice())) {
                SkuOrderDetailQuery skuOrderDetailQuery = new SkuOrderDetailQuery();
                skuOrderDetailQuery.setSkuOrderId(skuOrder.getId());
                List<SkuOrderDetail> list = skuOrderDetailDao.list(skuOrderDetailQuery);
                for (SkuOrderDetail skuOrderDetail : list) {
                    if ("Y".equals(skuOrderDetail.getIsModifyPrice())) {
                        payPrice += skuOrderDetail.getModifyPrice();
                    } else {
                        payPrice += skuOrderDetail.getPayPrice();
                    }
                }
                isModify = "Y";
            }
        } else if (outOrderId.startsWith(environment + OrderTypeEnum.CAR.getCode())) {
            Order order = orderDao.findById(orderId);
            if (order == null) {
                throw new BusinessException(ExceptionEnum.ERROR_1801.getCode(), ExceptionEnum.ERROR_1801.getMessage());
            }
            List<SkuOrder> skuOrderList = skuOrderDao.getListByOrderId(null, order.getId());
            if (skuOrderList.size() > 0) {
                for (SkuOrder skuOrder : skuOrderList) {
                    if (SkuOrderStatusEnum.CANCEL.getCode().equals(skuOrder.getStatus())) {
                        throw new BusinessException(ExceptionEnum.ERROR_1847.getCode(), ExceptionEnum.ERROR_1847.getMessage());
                    }
                    if ("Y".equals(skuOrder.getIsModifyPrice())) {
                        SkuOrderDetailQuery skuOrderDetailQuery = new SkuOrderDetailQuery();
                        skuOrderDetailQuery.setSkuOrderId(skuOrder.getId());
                        List<SkuOrderDetail> list = skuOrderDetailDao.list(skuOrderDetailQuery);
                        for (SkuOrderDetail skuOrderDetail : list) {
                            if ("Y".equals(skuOrderDetail.getIsModifyPrice())) {
                                payPrice += skuOrderDetail.getModifyPrice();
                            } else {
                                payPrice += skuOrderDetail.getPayPrice();
                            }
                        }
                        isModify = "Y";
                    } else {
                        SkuOrderDetailQuery skuOrderDetailQuery = new SkuOrderDetailQuery();
                        skuOrderDetailQuery.setSkuOrderId(skuOrder.getId());
                        List<SkuOrderDetail> list = skuOrderDetailDao.list(skuOrderDetailQuery);
                        for (SkuOrderDetail skuOrderDetail : list) {
                            SkuOrderDeliveryDemandQuery skuOrderDeliveryDemandQuery = new SkuOrderDeliveryDemandQuery();
                            skuOrderDeliveryDemandQuery.setLogistics(DeliveryWayEnum.FULLREDUCTION.getCode());
                            skuOrderDeliveryDemandQuery.setSkuOrderDetailId(skuOrderDetail.getId());
                            skuOrderDeliveryDemandQuery.setSkuOrderId(skuOrderDetail.getSkuOrderId());
                            skuOrderDeliveryDemandQuery.setNoPostageStatus(SkuOrderStatusEnum.CANCEL.getCode());
                            SkuOrderDeliveryDemand skuOrderDeliveryDemand = skuOrderDeliveryDemandDao.findFirst(skuOrderDeliveryDemandQuery);
                            if (skuOrderDeliveryDemand != null) {
                                payPrice += skuOrder.getPayPrice() + skuOrderDeliveryDemand.getPostage();
                            }
                        }
                    }
                }
            }
        }
        PayParam payParam = new PayParam();
        if ("Y".equals(isModify)) {
            PayOrder updateOrder = new PayOrder();
            updateOrder.setMoney(payPrice);
            updateOrder.setId(payOrder.getId());
            payOrderDao.updateById(updateOrder);
            payParam.setAmount(payPrice);
        } else {
            payParam.setAmount(payOrder.getMoney());
        }
        payParam.setOutOrderId(payOrder.getOutOrderId());
        payParam.setPrepareId(EnumPayMethod.FRIENDS.getCode() + "-" + payOrder.getId());
        payParam.setOrderId(payOrder.getId() + "");
        return payParam;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PayParam payOrderByEnjoy(PayReq req) throws BusinessException {
        PayParam payParam = new PayParam();
        PayOrder order = payOrderDao.findByMerchantIdAndOutOrderId(req.getUserId(), req.getOutOrderId());
        Date dateTime = DateUtil.getDateTime(req.getDate(), "yyyy-MM");
        Date startTime = DateUtil.getMinDateByMonth(dateTime);
        Date endTime = DateUtil.getMaxDayDate(DateUtil.getMaxDateByMonth(dateTime));
        SkuOrderQuery param = new SkuOrderQuery();
        param.setNoStatus(SkuOrderStatusEnum.CANCEL.getCode());
        param.setMerchantId(req.getUserId());
        param.setStartTime(startTime);
        param.setEndTime(endTime);
        param.setOrderType(SkuOrderTypeEnum.ENJOY.getCode());
        param.setIsPay("N");
        Integer count = skuOrderDao.count(param);
        if (count > 0) {
            Merchant merchant = merchantDao.findById(req.getUserId());
            DailiMerchantQuery dailiMerchantQuery = new DailiMerchantQuery();
            dailiMerchantQuery.setMobile(merchant.getMobile());
            Double enjoyPayPrice = skuOrderDao.getEnjoyPayPrice(req.getUserId(), startTime, endTime);
            List<Integer> skuOrderId = skuOrderDao.getSkuOrderId(param);
            for (Integer integer : skuOrderId) {
                SkuOrderDetailQuery skuOrderDetailQuery = new SkuOrderDetailQuery();
                skuOrderDetailQuery.setSkuOrderId(integer);
                List<SkuOrderDetail> list = skuOrderDetailDao.list(skuOrderDetailQuery);
                for (SkuOrderDetail skuOrderDetail : list) {
                    SkuOrderDeliveryDemandQuery skuOrderDeliveryDemandQuery = new SkuOrderDeliveryDemandQuery();
                    skuOrderDeliveryDemandQuery.setLogistics(DeliveryWayEnum.FULLREDUCTION.getCode());
                    skuOrderDeliveryDemandQuery.setSkuOrderDetailId(skuOrderDetail.getId());
                    skuOrderDeliveryDemandQuery.setSkuOrderId(skuOrderDetail.getSkuOrderId());
                    skuOrderDeliveryDemandQuery.setNoPostageStatus(SkuOrderStatusEnum.CANCEL.getCode());
                    SkuOrderDeliveryDemand skuOrderDeliveryDemand = skuOrderDeliveryDemandDao.findFirst(skuOrderDeliveryDemandQuery);
                    if (skuOrderDeliveryDemand != null) {
                        enjoyPayPrice += skuOrderDeliveryDemand.getPostage();
                    }
                }
            }

            Date date = new Date();
            if (order == null) {
                order = new PayOrder();
                order.setTitle(req.getSubject());
                order.setMoney(enjoyPayPrice);
                order.setOutOrderId(req.getOutOrderId());
                order.setMerchantId(req.getUserId());
                order.setPayMethod(req.getPayMethod());
                order.setIsPay("N");
                order.setCreateTime(date);
                payOrderDao.insert(order);
            } else {
                PayOrder updateOrder = new PayOrder();
                updateOrder.setTitle(req.getSubject());
                updateOrder.setMoney(enjoyPayPrice);
                updateOrder.setOutOrderId(req.getOutOrderId());
                updateOrder.setId(order.getId());
                payOrderDao.updateById(updateOrder);
            }
            if ("Y".equals(order.getIsPay())) {
                throw new BusinessException(ExceptionEnum.ERROR_1800.getCode(), ExceptionEnum.ERROR_1800.getMessage());
            }
            orderRelationDao.deleteByPayOrderId(order.getId());
            List<OrderRelation> list = new ArrayList<>();
            List<SkuOrder> skuOrderList = skuOrderDao.list(param);
            for (SkuOrder skuOrder : skuOrderList) {
                OrderRelation orderRelation = new OrderRelation();
                orderRelation.setPayOrderId(order.getId());
                orderRelation.setSkuOrderId(skuOrder.getId());
                orderRelation.setCreateTime(new Date());
                list.add(orderRelation);
            }
            orderRelationDao.insertBatch(list);
            payParam.setAmount(enjoyPayPrice);
            payParam.setOutOrderId(req.getOutOrderId());
            payParam.setSubject(req.getSubject());
            payParam.setPrepareId(req.getOrderTypeEnum().getCode() + "-" + order.getId());
            payParam.setPayMethod(req.getPayMethod());
            payParam.setOrderId(order.getId() + "");
        }
        return payParam;
    }

    @Override
    public FriendOrderResult findFriendOrderResultByOrderId(Integer merchantId, String outOrderId) throws
            BusinessException {
        FriendOrderResult friendOrderResult = new FriendOrderResult();
        PayOrder payOrder = payOrderDao.findByMerchantIdAndOutOrderId(merchantId, outOrderId);
        if (payOrder == null) {
            throw new BusinessException(ExceptionEnum.ERROR_1801.getCode(), ExceptionEnum.ERROR_1801.getMessage());
        }
        Integer orderId = null;
        String[] split = outOrderId.split("-");
        if (split.length > 1) {
            orderId = Integer.parseInt(split[1]);
        }
        if (orderId == null) {
            throw new BusinessException(ExceptionEnum.ERROR_1801.getCode(), ExceptionEnum.ERROR_1801.getMessage());
        }
        friendOrderResult.setOrderStatus(payOrder.getIsPay());
        Double postagePrice = 0.0;
        Double modifyPrice = 0.0;
        if (outOrderId.startsWith(environment + OrderTypeEnum.SKU.getCode()) || outOrderId.startsWith(environment + OrderTypeEnum.PERIODIZATION.getCode())) {
            SkuOrder skuOrder = skuOrderDao.findById(orderId);
            if (skuOrder == null) {
                throw new BusinessException(ExceptionEnum.ERROR_1801.getCode(), ExceptionEnum.ERROR_1801.getMessage());
            }
            SkuOrderResult skuOrderResult = new SkuOrderResult();
            BeanUtilExt.copyProperties(skuOrderResult, skuOrder);
            Order order = orderDao.findById(skuOrder.getOrderId());
            if (order == null) {
                throw new BusinessException(ExceptionEnum.ERROR_1801.getCode(), ExceptionEnum.ERROR_1801.getMessage());
            }
            if (SkuOrderStatusEnum.WAITING.getCode().equals(skuOrder.getStatus())) {
                PlatformSystemDeploy platformSystemDeploy = getPlatformSystemDeploy(SkuTypeEnum.SKU.getCode(), SkuOrderStatusEnum.WAITING.getCode());
                if (platformSystemDeploy != null) {
                    friendOrderResult.setExpireTime(getWebExpireTime(platformSystemDeploy.getUnit(), platformSystemDeploy.getDetails().intValue(), order.getCreateTime()));
                }
            } else if (SkuOrderStatusEnum.CANCEL.getCode().equals(skuOrder.getStatus())) {
                friendOrderResult.setOrderStatus(SkuOrderStatusEnum.CANCEL.getCode());
            }
            friendOrderResult.setPayPrice(skuOrderResult.getPayPrice());
            friendOrderResult.setReceiver(order.getReceiver());
            friendOrderResult.setReceiverMobile(order.getReceiverMobile());
            friendOrderResult.setAddress(order.getAddress());

            SkuOrderDetailQuery skuOrderDetailQuery = new SkuOrderDetailQuery();
            skuOrderDetailQuery.setSkuOrderId(skuOrderResult.getId());
            List<SkuOrderDetail> list = skuOrderDetailDao.list(skuOrderDetailQuery);
            List<SkuOrderDetailResult> skuOrderDetailResults = DTOUtils.convertList(list, SkuOrderDetailResult.class);
            for (SkuOrderDetailResult skuOrderDetailResult : skuOrderDetailResults) {
                if ("Y".equals(skuOrder.getIsSponsor())) {
                    SkuSalesInformation salesInformation = skuSalesInformationDao.findById(skuOrderDetailResult.getSkuSalesInformationId());
                    skuOrderDetailResult.setPrice(salesInformation.getSpellOrderPrice());
                }
                Sku sku = skuDao.findById(skuOrderDetailResult.getSkuId());
                if (sku != null) {
                    skuOrderDetailResult.setSkuName(sku.getTitle());
                    SkuContent skuContent = skuContentDao.findBySkuId(skuOrderDetailResult.getSkuId());
                    if (skuContent != null) {
                        skuOrderDetailResult.setCover(skuContent.getCover());
                        skuOrderResult.setSkuOrderDetailResults(skuOrderDetailResults);
                        if ("Y".equals(skuContent.getIsSpellOrder()) && SkuTypeEnum.PART.getCode().equals(skuOrderResult.getType())) {
                            SkuOrderQuery skuOrderQuery = new SkuOrderQuery();
                            skuOrderQuery.setPartSn(skuOrderResult.getPartSn());
                            skuOrderQuery.setNoStatus(SkuOrderStatusEnum.WAITING.getCode());
                            Integer scount = skuOrderDao.count(skuOrderQuery);
                            skuOrderResult.setFewpeople(skuContent.getSpellOrderPeople() - scount);
                        }

                    }
                    SkuCategory skuCategory = skuCategoryDao.findById(sku.getCategoryId());
                    if (skuCategory != null)
                        skuOrderDetailResult.setCategoryName(skuCategory.getName());
                }
                SkuOrderDetailPeriodizationQuery skuOrderDetailPeriodizationQuery = new SkuOrderDetailPeriodizationQuery();
                skuOrderDetailPeriodizationQuery.setSkuOrderDetailId(skuOrderDetailResult.getId());
                SkuOrderDetailPeriodization skuOrderDetailPeriodization = skuOrderDetailPeriodizationDao.findFirst(skuOrderDetailPeriodizationQuery);
                if (skuOrderDetailPeriodization != null) {
                    skuOrderDetailResult.setExtractSpecifications(skuOrderDetailPeriodization.getExtractSpecifications());
                }
                SkuOrderDeliveryDemandQuery skuOrderDeliveryDemandQuery = new SkuOrderDeliveryDemandQuery();
                skuOrderDeliveryDemandQuery.setLogistics(DeliveryWayEnum.FULLREDUCTION.getCode());
                skuOrderDeliveryDemandQuery.setSkuOrderDetailId(skuOrderDetailResult.getId());
                skuOrderDeliveryDemandQuery.setSkuOrderId(skuOrderDetailResult.getSkuOrderId());
                skuOrderDeliveryDemandQuery.setNoPostageStatus(SkuOrderStatusEnum.CANCEL.getCode());
                SkuOrderDeliveryDemand skuOrderDeliveryDemand = skuOrderDeliveryDemandDao.findFirst(skuOrderDeliveryDemandQuery);
                if (skuOrderDeliveryDemand != null) {
                    postagePrice += skuOrderDeliveryDemand.getPostage();
                }
                if ("Y".equals(skuOrderDetailResult.getIsModifyPrice())) {
                    modifyPrice += skuOrderDetailResult.getModifyPrice();
                } else {
                    modifyPrice += skuOrderDetailResult.getPayPrice();
                }
            }
            friendOrderResult.setPostagePrice(postagePrice);
            friendOrderResult.setPayPrice(skuOrderResult.getPayPrice() + postagePrice);
            List<SkuOrderResult> skuOrderResults = new ArrayList<>();
            skuOrderResults.add(skuOrderResult);
            friendOrderResult.setSkuOrderResults(skuOrderResults);
            if ("Y".equals(skuOrder.getIsModifyPrice())) {
                friendOrderResult.setModifyPrice(modifyPrice);
            }
        } else if (outOrderId.startsWith(environment + OrderTypeEnum.CAR.getCode())) {
            Order order = orderDao.findById(orderId);
            if (order == null) {
                throw new BusinessException(ExceptionEnum.ERROR_1801.getCode(), ExceptionEnum.ERROR_1801.getMessage());
            }
            Double payPrice = 0.0;

            friendOrderResult.setReceiver(order.getReceiver());
            friendOrderResult.setReceiverMobile(order.getReceiverMobile());
            friendOrderResult.setAddress(order.getAddress());
            List<SkuOrder> skuOrderList = skuOrderDao.getListByOrderId(null, order.getId());
            List<SkuOrderResult> skuOrderResults = DTOUtils.convertList(skuOrderList, SkuOrderResult.class);
            for (SkuOrderResult skuOrderResult : skuOrderResults) {
                postagePrice = 0.0;
                if (SkuOrderStatusEnum.CANCEL.getCode().equals(skuOrderResult.getStatus()) || !SkuOrderStatusEnum.WAITING.getCode().equals(skuOrderResult.getStatus())) {
                    friendOrderResult.setOrderStatus(SkuOrderStatusEnum.CANCEL.getCode());
                }
                if (SkuOrderStatusEnum.WAITING.getCode().equals(skuOrderResult.getStatus())) {
                    PlatformSystemDeploy platformSystemDeploy = getPlatformSystemDeploy(SkuTypeEnum.SKU.getCode(), SkuOrderStatusEnum.WAITING.getCode());
                    if (platformSystemDeploy != null) {
                        friendOrderResult.setExpireTime(getWebExpireTime(platformSystemDeploy.getUnit(), platformSystemDeploy.getDetails().intValue(), order.getCreateTime()));
                    }
                }
                SkuOrderDetailQuery skuOrderDetailQuery = new SkuOrderDetailQuery();
                skuOrderDetailQuery.setSkuOrderId(skuOrderResult.getId());
                List<SkuOrderDetail> list = skuOrderDetailDao.list(skuOrderDetailQuery);
                List<SkuOrderDetailResult> skuOrderDetailResults = DTOUtils.convertList(list, SkuOrderDetailResult.class);
                for (SkuOrderDetailResult skuOrderDetailResult : skuOrderDetailResults) {
                    Sku sku = skuDao.findById(skuOrderDetailResult.getSkuId());
                    if (sku != null) {
                        skuOrderDetailResult.setSkuName(sku.getTitle());
                        SkuContent skuContent = skuContentDao.findBySkuId(skuOrderDetailResult.getSkuId());
                        if (skuContent != null) {
                            skuOrderDetailResult.setCover(skuContent.getCover());
                            skuOrderResult.setSkuOrderDetailResults(skuOrderDetailResults);
                            if ("Y".equals(skuContent.getIsSpellOrder()) && SkuTypeEnum.PART.getCode().equals(skuOrderResult.getType())) {
                                SkuOrderQuery skuOrderQuery = new SkuOrderQuery();
                                skuOrderQuery.setPartSn(skuOrderResult.getPartSn());
                                skuOrderQuery.setNoStatus(SkuOrderStatusEnum.WAITING.getCode());
                                Integer scount = skuOrderDao.count(skuOrderQuery);
                                skuOrderResult.setFewpeople(skuContent.getSpellOrderPeople() - scount);
                            }

                        }
                        SkuCategory skuCategory = skuCategoryDao.findById(sku.getCategoryId());
                        if (skuCategory != null)
                            skuOrderDetailResult.setCategoryName(skuCategory.getName());
                        SkuOrderDeliveryDemandQuery skuOrderDeliveryDemandQuery = new SkuOrderDeliveryDemandQuery();
                        skuOrderDeliveryDemandQuery.setLogistics(DeliveryWayEnum.FULLREDUCTION.getCode());
                        skuOrderDeliveryDemandQuery.setSkuOrderDetailId(skuOrderDetailResult.getId());
                        skuOrderDeliveryDemandQuery.setSkuOrderId(skuOrderDetailResult.getSkuOrderId());
                        skuOrderDeliveryDemandQuery.setNoPostageStatus(SkuOrderStatusEnum.CANCEL.getCode());
                        SkuOrderDeliveryDemand skuOrderDeliveryDemand = skuOrderDeliveryDemandDao.findFirst(skuOrderDeliveryDemandQuery);
                        if (skuOrderDeliveryDemand != null) {
                            postagePrice += skuOrderDeliveryDemand.getPostage();
                        }
                    }
                    if ("Y".equals(skuOrderDetailResult.getIsModifyPrice())) {
                        payPrice += skuOrderDetailResult.getModifyPrice();
                    } else {
                        payPrice += skuOrderDetailResult.getPayPrice() + postagePrice;

                    }
                }
                if ("Y".equals(skuOrderResult.getIsModifyPrice())) {
                    skuOrderResult.setModifyPrice(payPrice);
                }
            }
            if (order.getPayPrice() != payPrice) {
                friendOrderResult.setPayPrice(order.getPayPrice());
                friendOrderResult.setModifyPrice(payPrice);
            }
            friendOrderResult.setSkuOrderResults(skuOrderResults);
        } else if (outOrderId.startsWith(environment + OrderTypeEnum.ENJOY.getCode())) {
            SkuOrder skuOrder = skuOrderDao.findById(orderId);
            if (skuOrder == null) {
                throw new BusinessException(ExceptionEnum.ERROR_1801.getCode(), ExceptionEnum.ERROR_1801.getMessage());
            }
            SkuOrderResult skuOrderResult = new SkuOrderResult();
            BeanUtilExt.copyProperties(skuOrderResult, skuOrder);
            Order order = orderDao.findById(skuOrder.getOrderId());
            if (order == null) {
                throw new BusinessException(ExceptionEnum.ERROR_1801.getCode(), ExceptionEnum.ERROR_1801.getMessage());
            }
            if (SkuOrderTypeEnum.ENJOY.getCode().equals(skuOrder.getOrderType())
                    && "N".equals(skuOrder.getIsPay())
                    && !SkuOrderStatusEnum.CANCEL.getCode().equals(skuOrder.getStatus())) {
                friendOrderResult.setExpireTime(getEnjoyEndTime(order.getCreateTime()));
            } else if (SkuOrderStatusEnum.CANCEL.getCode().equals(skuOrder.getStatus())) {
                friendOrderResult.setOrderStatus(SkuOrderStatusEnum.CANCEL.getCode());
            }
            friendOrderResult.setPayPrice(skuOrderResult.getPayPrice());
            friendOrderResult.setReceiver(order.getReceiver());
            friendOrderResult.setReceiverMobile(order.getReceiverMobile());
            friendOrderResult.setAddress(order.getAddress());

            SkuOrderDetailQuery skuOrderDetailQuery = new SkuOrderDetailQuery();
            skuOrderDetailQuery.setSkuOrderId(skuOrderResult.getId());
            List<SkuOrderDetail> list = skuOrderDetailDao.list(skuOrderDetailQuery);
            List<SkuOrderDetailResult> skuOrderDetailResults = DTOUtils.convertList(list, SkuOrderDetailResult.class);
            for (SkuOrderDetailResult skuOrderDetailResult : skuOrderDetailResults) {
                if ("Y".equals(skuOrder.getIsSponsor())) {
                    SkuSalesInformation salesInformation = skuSalesInformationDao.findById(skuOrderDetailResult.getSkuSalesInformationId());
                    skuOrderDetailResult.setPrice(salesInformation.getSpellOrderPrice());
                }
                Sku sku = skuDao.findById(skuOrderDetailResult.getSkuId());
                if (sku != null) {
                    skuOrderDetailResult.setSkuName(sku.getTitle());
                    SkuContent skuContent = skuContentDao.findBySkuId(skuOrderDetailResult.getSkuId());
                    if (skuContent != null) {
                        skuOrderDetailResult.setCover(skuContent.getCover());
                        skuOrderResult.setSkuOrderDetailResults(skuOrderDetailResults);
                        if ("Y".equals(skuContent.getIsSpellOrder()) && SkuTypeEnum.PART.getCode().equals(skuOrderResult.getType())) {
                            SkuOrderQuery skuOrderQuery = new SkuOrderQuery();
                            skuOrderQuery.setPartSn(skuOrderResult.getPartSn());
                            skuOrderQuery.setNoStatus(SkuOrderStatusEnum.WAITING.getCode());
                            Integer scount = skuOrderDao.count(skuOrderQuery);
                            skuOrderResult.setFewpeople(skuContent.getSpellOrderPeople() - scount);
                        }

                    }
                    SkuCategory skuCategory = skuCategoryDao.findById(sku.getCategoryId());
                    if (skuCategory != null)
                        skuOrderDetailResult.setCategoryName(skuCategory.getName());
                    SkuOrderDeliveryDemandQuery skuOrderDeliveryDemandQuery = new SkuOrderDeliveryDemandQuery();
                    skuOrderDeliveryDemandQuery.setLogistics(DeliveryWayEnum.FULLREDUCTION.getCode());
                    skuOrderDeliveryDemandQuery.setSkuOrderDetailId(skuOrderDetailResult.getId());
                    skuOrderDeliveryDemandQuery.setSkuOrderId(skuOrderDetailResult.getSkuOrderId());
                    skuOrderDeliveryDemandQuery.setNoPostageStatus(SkuOrderStatusEnum.CANCEL.getCode());
                    SkuOrderDeliveryDemand skuOrderDeliveryDemand = skuOrderDeliveryDemandDao.findFirst(skuOrderDeliveryDemandQuery);
                    if (skuOrderDeliveryDemand != null) {
                        skuOrderResult.setPayPrice(skuOrderDetailResult.getPayPrice() + skuOrderDeliveryDemand.getPostage());
                    }
                }
            }
            List<SkuOrderResult> skuOrderResults = new ArrayList<>();
            skuOrderResults.add(skuOrderResult);
            friendOrderResult.setSkuOrderResults(skuOrderResults);
        } else {
            throw new BusinessException(ExceptionEnum.ERROR_1865.getCode(), ExceptionEnum.ERROR_1865.getMessage());
        }
        return friendOrderResult;
    }


    PlatformSystemDeploy getPlatformSystemDeploy(String type, String status) {
        PlatformSystemDeployQuery platformSystemDeployQuery = new PlatformSystemDeployQuery();
        platformSystemDeployQuery.setType(type);
        platformSystemDeployQuery.setStatus(status);
        PlatformSystemDeploy platformSystemDeploy = platformSystemDeployDao.findFirst(platformSystemDeployQuery);
        return platformSystemDeploy;
    }

    public Date getWebExpireTime(String uint, Integer details, Date date) {
        if (SystemDeployTimeEnum.DAY.getCode().equals(uint)) {
            return DateUtil.addDays(date, details);
        } else if (SystemDeployTimeEnum.HOUR.getCode().equals(uint)) {
            return DateUtil.addHour(date, details);
        } else if (SystemDeployTimeEnum.MINUTE.getCode().equals(uint)) {
            return DateUtil.addMinutes(date, details);
        }
        return null;
    }

    public Date getEnjoyEndTime(Date date) {
        int enjoyDay = Integer.parseInt(enjouyDay);
        int monthInteger = DateUtil.getMonthInteger(date);
        return DateUtil.setMinDate(DateUtil.getDate(DateUtil.getYear(), monthInteger + 1, enjoyDay));

    }

}
